// 单个商品总价计算
function totalPrice() {
	var snum = parseFloat($(this).parent().siblings('input').val());
	var sprice = parseFloat($(this).parents('.shopcart-item-num').siblings('.shopcart-item-unit-price').find('span')
		.text());
	var singletotal = snum * sprice;
	$(this).parents('.shopcart-item-num').siblings('.shopcart-item-unit-total-price').find('span').text(singletotal);
}
// 选种中商品总价
function totalprices() {
	var asum = 0;
	$('.checkitem div').each(function(index, item) {
		if ($(item).hasClass('checked')) {
			asum += parseFloat($(item).parents('ul').find('.shopcart-item-unit-total-price').children().text());
		}
	})
	$('.shopcart-c-bottom-sum-totalprices').text(asum);
}
// 选中商品数量
function summation() {
	var gsum = 0;
	$('.checkitem div').each(function(index, item) {
		if ($(item).hasClass('checked')) {
			gsum += parseFloat($(item).parents('ul').find('input').val());
		}
	})
	$('.shopcart-c-bottom-choose-goodsnum').text(gsum);
}
// 头部全选框
$('.shopcart-c').on('click','.shopcart-c-kind-checkall',function() {
	if ($(this).children().hasClass('unchecked')) {
		$(this).children().removeClass('unchecked').addClass('checked');
		$('.checkitem').children().removeClass('unchecked').addClass('checked');
		$('.shopcart-c-bottom-checkall').children().removeClass('unchecked').addClass('checked');
	} else {
		$(this).children().removeClass('checked').addClass('unchecked');
		$('.checkitem').children().removeClass('checked').addClass('unchecked');
		$('.shopcart-c-bottom-checkall').children().removeClass('checked').addClass('unchecked');
	}
	var asum = 0;
	$('.checkitem div').each(function(index, item) {
		if ($(item).hasClass('checked')) {
			asum += parseFloat($(item).parents('ul').find('.shopcart-item-unit-total-price').children()
				.text());
		}
	})
	$('.shopcart-c-bottom-sum-totalprices').text(asum);
	var gsum = 0;
	$('.checkitem div').each(function(index, item) {
		if ($(item).hasClass('checked')) {
			gsum += parseFloat($(item).parents('ul').find('input').val());
		}
	})
	$('.shopcart-c-bottom-choose-goodsnum').text(gsum);
})
// 单品选择框
$('.shopcart-c').on('click','.checkitem',function() {
	if ($(this).children().hasClass('unchecked')) {
		$(this).children().removeClass('unchecked').addClass('checked');
	} else {
		$(this).children().removeClass('checked').addClass('unchecked');
	}
	var total = $('.checkitem').length;
	var num = $('.checkitem .checked').length;
	console.log(num);
	console.log(total);
	if (num == total) {
		$('.shopcart-c-bottom-checkall').children().removeClass('unchecked').addClass('checked');
		$('.shopcart-c-kind-checkall').children().removeClass('unchecked').addClass('checked');
	} else {
		$('.shopcart-c-bottom-checkall').children().removeClass('checked').addClass('unchecked');
		$('.shopcart-c-kind-checkall').children().removeClass('checked').addClass('unchecked');
	}
	var asum = 0;
	$('.checkitem div').each(function(index, item) {
		if ($(item).hasClass('checked')) {
			asum += parseFloat($(item).parents('ul').find('.shopcart-item-unit-total-price').children()
				.text());
		}
	})
	$('.shopcart-c-bottom-sum-totalprices').text(asum);
	var gsum = 0;
	$('.checkitem div').each(function(index, item) {
		if ($(item).hasClass('checked')) {
			gsum += parseFloat($(item).parents('ul').find('input').val());
		}
	})
	$('.shopcart-c-bottom-choose-goodsnum').text(gsum);
})

// 底部全选框
$('.shopcart-c').on('click','.shopcart-c-bottom-checkall',function() {
	if ($(this).children().hasClass('unchecked')) {
		$(this).children().removeClass('unchecked').addClass('checked');
		$('.checkitem').children().removeClass('unchecked').addClass('checked');
		$('.shopcart-c-kind-checkall').children().removeClass('unchecked').addClass('checked');
	} else {
		$(this).children().removeClass('checked').addClass('unchecked');
		$('.checkitem').children().removeClass('checked').addClass('unchecked');
		$('.shopcart-c-kind-checkall').children().removeClass('checked').addClass('unchecked');
	}
	var asum = 0;
	$('.checkitem div').each(function(index, item) {
		if ($(item).hasClass('checked')) {
			asum += parseFloat($(item).parents('ul').find('.shopcart-item-unit-total-price').children()
				.text());
		}
	})
	$('.shopcart-c-bottom-sum-totalprices').text(asum);
	var gsum = 0;
	$('.checkitem div').each(function(index, item) {
		if ($(item).hasClass('checked')) {
			gsum += parseFloat($(item).parents('ul').find('input').val());
		}
	})
	$('.shopcart-c-bottom-choose-goodsnum').text(gsum);
})
// 数量增减
$('.shopcart-c').on('click','.shopcart-item-num-c-btn>a:last-child',function() {
	var num = parseInt($(this).parent().siblings('input').val());

	var a = ++num;
	$(this).parent().siblings('input').val(a);
	$(this).siblings().css({
		color: ' #777',
		cursor: 'pointer'
	})
	var snum = parseFloat($(this).parent().siblings('input').val());
	var sprice = parseFloat($(this).parents('.shopcart-item-num').siblings('.shopcart-item-unit-price').find(
		'span').text());
	var singletotal = snum * sprice;
	$(this).parents('.shopcart-item-num').siblings('.shopcart-item-unit-total-price').find('span').text(
		singletotal);
	var asum = 0;
	$('.checkitem div').each(function(index, item) {
		if ($(item).hasClass('checked')) {
			asum += parseFloat($(item).parents('ul').find('.shopcart-item-unit-total-price').children()
				.text());
		}
	})
	$('.shopcart-c-bottom-sum-totalprices').text(asum);
	var gsum = 0;
	$('.checkitem div').each(function(index, item) {
		if ($(item).hasClass('checked')) {
			gsum += parseFloat($(item).parents('ul').find('input').val());
		}
	})
	$('.shopcart-c-bottom-choose-goodsnum').text(gsum);


})
$('.shopcart-c').on('click','.shopcart-item-num-c-btn a:first-child',function() {
	var num = parseInt($(this).parent().siblings('input').val());
	if (num == 1) {
		$(this).parent().siblings('input').val(1);
		$(this).css({
			color: ' #c4c4c4',
			cursor: 'not-allowed'
		})

		return;
	}
	var a = --num;
	$(this).parent().siblings('input').val(a);
	var snum = parseFloat($(this).parent().siblings('input').val());
	var sprice = parseFloat($(this).parents('.shopcart-item-num').siblings('.shopcart-item-unit-price').find(
		'span').text());
	var singletotal = snum * sprice;
	$(this).parents('.shopcart-item-num').siblings('.shopcart-item-unit-total-price').find('span').text(
		singletotal);
	var asum = 0;
	$('.checkitem div').each(function(index, item) {
		if ($(item).hasClass('checked')) {
			asum += parseFloat($(item).parents('ul').find('.shopcart-item-unit-total-price').children()
				.text());
		}
	})
	$('.shopcart-c-bottom-sum-totalprices').text(asum);
	var gsum = 0;
	$('.checkitem div').each(function(index, item) {
		if ($(item).hasClass('checked')) {
			gsum += parseFloat($(item).parents('ul').find('input').val());
		}
	})
	$('.shopcart-c-bottom-choose-goodsnum').text(gsum);
})
// 单品删除
$('.shopcart-c').on('click','.shopcart-item-del',function() {
	$(this).parents('.shopcart-item').remove();
})
// 全删
$('.shopcart-c').on('click','.shopcart-c-bottom-delall',function() {
	$('.shopcart-detail').remove();
})

function getCookie(cname) {
	var name = cname + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i].trim();
		var arrC = c.split("=");
		if (arrC[0] == cname) {
			return arrC[1];
		}
	}
	return "";
}
$(document).ready(function() {
	// 读取cookie
	var cart = getCookie('cart');
	if (cart) {
		var goodsList = JSON.parse(cart);
		console.log(goodsList)
		var html = '';
		goodsList.forEach(function(item, index) {
			var total=parseFloat(item.price) * parseFloat(item.num)
			html +=
				'<div class="shopcart-item"><ul class="clear-fix"><li class="fl checkitem"><div class="unchecked"></div></li><li class="fl shopcart-item-img"><img src="' +
				item.photo +
				'" ></li><li class="fl shopcart-item-intro"><div><a href="" target="_blank">' + item
				.name +
				'</a></div><div>运动版 幻夜黑</div><div>分期免息</div></li><li class="shopcart-item-unit-price fl">¥ <span>' +
				item.price +
				'</span></li><li class="shopcart-item-num fl"><div class="shopcart-item-num-c"><input type="num" name="" id="" value="' +
				item.num +
				'" /><p class="shopcart-item-num-c-btn clear-fix"><a href="javascript:;" class="shopcart-item-num-c-btn-minus">-</a><a href="javascript:;" class="shopcart-item-num-c-btn-plus">+</a></p></div></li><li class="shopcart-item-unit-total-price fl">¥ <span>'+total+'</span></li><li class="shopcart-item-del fl"><a href="javascript:;">删除</a></li></ul></div>'
		})
		$('.shopcart-detail').html(html);
	}
})
